//
//  QYNetManager.m
//  qianyingFM
//
//  Created by yangyang on 16/4/16.
//  Copyright © 2016年 杨阳. All rights reserved.
//

#import "QYNetManager.h"

@interface QYNetManager()

 
@end

@implementation QYNetManager

/**
 *  获取网络管理的单例
 *
 */
+(instancetype)sharedManager {
    static QYNetManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[QYNetManager alloc]init];
    });
    return manager;
}

- (instancetype)init {
    if (self = [super init]) {
        _afhttpManager = [AFHTTPSessionManager manager];
        _afhttpManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                             @"text/html",
                                                             @"image/jpeg",
                                                             @"image/png",
                                                             @"application/octet-stream",
                                                             @"text/json",
                                                             @"text/plain",
                                                             nil];
        _afhttpManager.requestSerializer.timeoutInterval = 15.f;
    }
    return self;
}

/**
 *  get 请求
 *
 */

- (void)GET:(NSString*)url parameters:(id)parameters SuccessBlock:(SuccessBlock)block Failure:(Failure)failuerBlock {
    
    [_afhttpManager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //请求成功
        block(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败
        failuerBlock(error);
        
    }];
}

//post请求
- (void)POST:(NSString*)url parameters:(id)parameters andApiTyp:(CustomerApiTyp)type SuccessBlock:(SuccessBlock)block Failure:(Failure)failuerBlock{
    
    NSString * urlStr;
    switch (type) {
        case CustomerApiTypMain:
            urlStr = [NSString stringWithFormat:@"%@%@",kServiceID,url];
            break;
        case CustomerApiTypGetOrders:
            urlStr = [NSString stringWithFormat:@"%@%@",kOrdersServiceID,url];
            break;
        default:
            break;
    }
    NSLog(@"RequestParameters>>>%@\nRequestURL>>>>%@",parameters,urlStr);
    [_afhttpManager POST:urlStr parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //请求成功
        NSLog(@"%@ >>>>> %@",urlStr,responseObject);
        if ([responseObject[@"error"] isEqualToString:@"token failed"]) {
            [self performSelector:@selector(tokenFailed) withObject:nil afterDelay:1.5f];
        }
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败
        failuerBlock(error);
    }];
}

-(void)tokenFailed{
    if (self->_ErrorForTokenFailed) {
        self->_ErrorForTokenFailed();
    }
}

-(void)upLoadSingeImage:(NSString *)url parameters:(id)parameters andImage:(UIImage *)image andImageName:(NSString *)nowTime SuccessBlock:(SuccessBlock)block Failure:(Failure)failuerBlock{
    
    [_afhttpManager POST:url parameters: parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
        
        //NSDictionary * dic = parameters;
        //NSString * key = dic[@"file"];
//        NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
        
        NSData *imageData = [HSPublic hsZipDataWithImage:[HSPublic imageByScalingToMaxSize:image]];
        
        NSString * fileName = [NSString stringWithFormat:@"%@.jpg", nowTime];
        [formData appendPartWithFileData:imageData
                                    name:@"file"
                                fileName:fileName
                                mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress *_Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
        //请求成功
        block(responseObject);
    } failure:^(NSURLSessionDataTask *_Nullable task, NSError * _Nonnull error) {
        //请求失败
        failuerBlock(error);
    }];
    
}
/*
 
 */

-(void)upLoadImageWithURL:(NSString *)url parameters:(id)parameters andImageInfoArr:(NSArray *)imageInfoArr SuccessBlock:(SuccessBlock)block Failure:(Failure)failuerBlock{
    
    [_afhttpManager POST:url parameters: parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
        NSLog(@"%@",parameters);
        for (NSInteger i = 0; i < [imageInfoArr.firstObject count]; i++) {
            
            UIImage *image = imageInfoArr[0][i];
            NSData *imageData = [HSPublic hsZipDataWithImage:[HSPublic imageByScalingToMaxSize:image]];
            
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyyMMddHHmmss"];
            PHAsset *assets = imageInfoArr[1][i];
            NSString *currentTimeString = [formatter stringFromDate:assets.creationDate];
            
            NSString * fileName = [NSString stringWithFormat:@"%@.jpg",currentTimeString];
            [formData appendPartWithFileData:imageData
                                        name:@"file"
                                    fileName:fileName
                                    mimeType:@"image/jpeg"];
            
        }
        
    } progress:^(NSProgress *_Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
        //请求成功
        block(responseObject);
    } failure:^(NSURLSessionDataTask *_Nullable task, NSError * _Nonnull error) {
        //请求失败
        failuerBlock(error);
    }];
    
}

@end
